package com.zhouqinuan.spring.cloud.develop.framework.demo.rabbitmq.controller;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhouqinuan.spring.cloud.develop.framework.demo.rabbitmq.message.exchange.RabbitExchange;
import com.zhouqinuan.spring.cloud.develop.framework.demo.rabbitmq.message.routingkey.RabbitRoutingKey;
import com.zhouqinuan.spring.cloud.develop.framework.demo.rabbitmq.model.UserInfo;
import com.zhouqinuan.spring.cloud.develop.framework.demo.rabbitmq.service.UserService;

import io.swagger.annotations.Api;

@Api(tags = "Rabbitmq测试例子")
@RestController
public class RabbitmqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserService userService;

    @GetMapping(path = "/send/user")
    public String sendUser(@RequestParam Long uid) {

        UserInfo userInfo = userService.getUserInfo(uid);

        rabbitTemplate.convertAndSend(RabbitExchange.USER_MESSAGE_EXCHANGE_DIRECT,
                RabbitRoutingKey.USER_MESSAGE_ROUTING_KEY, userInfo);

        rabbitTemplate.convertAndSend(RabbitExchange.USER_MESSAGE_EXCHANGE_DIRECT_DELAY,
                RabbitRoutingKey.USER_MESSAGE_ROUTING_KEY_DELAY, userInfo, new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {

                        message.getMessageProperties().setDelay(5000);

                        return message;
                    }
                });

        return "ok";
    }
}
